package demo.admin.common;

import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

/**
 * 统一返回结果
 */
@Data
public class BaseResult<T> implements Serializable {

    private Integer code;
    private String message;
    private Long count;
    private T data;

    public BaseResult() {
        this.setCode(ResultCode.SUCCESS.getCode());
        this.setMessage(ResultCode.SUCCESS.getMsg());
    }

    public BaseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }


    public static <T> BaseResult success(T data) {
        BaseResult baseResult = new BaseResult();
        if (Objects.isNull(data)) {
            String s = "";
            baseResult.setData(s);
        } else {
            baseResult.setData(data);
        }
        return baseResult;
    }

    public static <T> BaseResult success(T data, Long count) {
        BaseResult baseResult = new BaseResult();
        if (Objects.isNull(data)) {
            String s = "";
            baseResult.setData(s);
        } else {
            baseResult.setData(data);
        }
        baseResult.setCount(count);
        return baseResult;
    }

    public static <T> BaseResult success(T data, Integer count) {
        BaseResult baseResult = new BaseResult();
        if (Objects.isNull(data)) {
            String s = "";
            baseResult.setData(s);
        } else {
            baseResult.setData(data);
        }
        baseResult.setCount(Long.valueOf(count));
        return baseResult;
    }

    public static BaseResult error(ResultCode resultCode) {
        BaseResult baseResult = new BaseResult();
        baseResult.setCode(resultCode.getCode());
        baseResult.setMessage(resultCode.getMsg());
        return baseResult;
    }

    public static <T> BaseResult error(ResultCode resultCode, T data) {
        BaseResult baseResult = new BaseResult();
        baseResult.setCode(resultCode.getCode());
        baseResult.setMessage(resultCode.getMsg());
        baseResult.setData(data);
        return baseResult;
    }


}
